Developer Documentation

QuickTime 4 API Documentation

QuickTime for Java

| Previous | Chapter Top | Next |

Play a Streaming Movie

Example 0.5 builds on the QTSimpleApplet code discussed in "The QTSimpleApplet Code" . This applet enables you to play a steaming movie from a URL.

You define the instance variables for the applet:

private Drawable myQTContent;
private QTCanvas myQTCanvas;

Just as with the QTSimpleApplet code sample, you can use the standard init() , start() , stop() , and destroy() methods to initialize, execute, and terminate the applet. Likewise, you call QTSession.open() in order to make sure that QuickTime is present and initialized. Again, this is a required call before any QuickTime for Java classes can be used. It is called first in the init() method. In order to shut down QuickTime properly, you also need to call QTSession.close() if you have previously called QTSession.open() . This is called in the destroy() method.

QTCanvas , as we've seen, is a display space into which QuickTime can draw and receive events. QTCanvas provides the output destination for QuickTime drawing. You set up a QTCanvas to display its content at its original size or smaller and centered in the space given to the QTCanvas when the applet is laid out. The QTCanvas is initialized to display its client up to as large as that client's initial size. And 0.5F flags are used to position the canvas at the center of the space allocated to it by its parent container's layout manager:

setLayout (new BorderLayout());
myQTCanvas = new QTCanvas (QTCanvas.kInitialSize, 0.5F, 0.5F);
add (myQTCanvas, "Center");

You need to set the client as a Drawable object that can display into the canvas. The QuickTime logo is displayed when there is no movie to display. Thus, ImageDrawer is set up as the initial client of QTCanvas .

myQTContent = ImageDrawer.getQTLogo();

You enter the URL to a QuickTime movie to be displayed in a text field:

final TextField urlTextField = new TextField (
                            "file:///... Enter an URL to a movie",
                            30);

You set the font and font size in the text field for the URL. The initial string is displayed in the text field. You add an ActionListener so that the events taking place on the text field are captured and executed. tf.getText() returns the URL that the user has entered:

urlTextField.setFont (new Font ("Dialog", Font.PLAIN, 10));
urlTextField.setEditable (true);
urlTextField.addActionListener (new ActionListener () {
    TextField tf = urlTextField;

    public void actionPerformed (ActionEvent ae) {
        myQTContent = QTFactory.makeDrawable (tf.getText());
        myQTCanvas.setClient (myQTContent, true);
    }
});

The URL can support the following protocols:

The URL can also point to any media file (movies, images, and so on) that QuickTime can present. The single makeDrawable() method will return the appropriate QuickTime object to present the specified media. Once created, this QuickTime object is set as the client of the QTCanvas and can then be viewed and/or played by the user.

Note that no error handling is done in the code in Example 0.5 .

QTStreamingApplet.java

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

import quicktime.*;
import quicktime.io.QTFile;

import quicktime.app.QTFactory;
import quicktime.app.display.*;
import quicktime.app.image.ImageDrawer;

public class QTStreamingApplet extends Applet {
    private Drawable myQTContent;
    private QTCanvas myQTCanvas;
    
    public void init () {
        try {
            QTSession.open();
            setLayout (new BorderLayout());
            myQTCanvas = new QTCanvas (QTCanvas.kInitialSize, 0.5F, 0.5F);
            add (myQTCanvas, "Center");     
            
            myQTContent = ImageDrawer.getQTLogo();
        
            final TextField urlTextField = new TextField ("Enter URL to movie here",
                                                            30);
            urlTextField.setFont (new Font ("Dialog", Font.PLAIN, 10));
            urlTextField.setEditable (true);
            urlTextField.addActionListener (new ActionListener () {
                TextField tf = urlTextField;
                
                public void actionPerformed (ActionEvent ae) {
                    if (myQTCanvas != null) {
                        try {
                            myQTContent = QTFactory.makeDrawable (tf.getText());
                            myQTCanvas.setClient (myQTContent, true);
                        } catch (QTException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            add (urlTextField, "South");
        } catch (QTException qtE) {
            throw new RuntimeException (qtE.getMessage());      
        }
    }   

    public void start () {
        try {
            if (myQTCanvas != null)
                myQTCanvas.setClient (myQTContent, true);           
        } catch (QTException e) {
            e.printStackTrace();
        }
    }
    
    public void stop () {
        if (myQTCanvas != null)
            myQTCanvas.removeClient();
    }
    
    public void destroy () {
        QTSession.close();
    }
}

© 1999 Apple Computer, Inc.

| Previous | Chapter Top | Next |